using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;

/****************************************************
// 功能：打包IOS会自动调用这个类加载依赖文件
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/1/9 14:1:46
*****************************************************/

public class IosAutoBuild
{
    [PostProcessBuild(9999)]
    public static void OnPostProcessBuild(BuildTarget target, string pathToBuildProject)
    {
        if (target != BuildTarget.iOS)
            return;

        string projPath = pathToBuildProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
        PBXProject proj = new PBXProject();
        proj.ReadFromFile(projPath);

        string targetGuid = proj.GetUnityMainTargetGuid();
        
        //添加Other Linker Flags
        proj.AddBuildProperty(targetGuid, "OTHER_LDFLAGS", "-fobjc-arc");
        proj.AddBuildProperty(targetGuid, "OTHER_LDFLAGS", "-Objc -all_load");

        //修改部分设置
        proj.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "NO");

        //添加代码库
        proj.AddFileToBuild(targetGuid,
            proj.AddFile("usr/lib/libiconv.tbd", "Frameworks/libiconv.tbd", PBXSourceTree.Sdk));
        proj.AddFileToBuild(targetGuid,
            proj.AddFile("usr/lib/libsqlite3.tbd", "Frameworks/libsqlite3.tbd", PBXSourceTree.Sdk));
        proj.AddFileToBuild(targetGuid, proj.AddFile("usr/lib/libc++.tbd", "Frameworks/libc++.tbd", PBXSourceTree.Sdk));
        proj.AddFileToBuild(targetGuid, proj.AddFile("usr/lib/libz.tbd", "Frameworks/libz.tbd", PBXSourceTree.Sdk));

        //添加Frameworks
        proj.AddFrameworkToProject(targetGuid, "Security.framework", false);
        proj.AddFrameworkToProject(targetGuid, "SystemConfiguration.framework", false);
        proj.AddFrameworkToProject(targetGuid, "CoreGraphics.Framework", false);
        proj.AddFrameworkToProject(targetGuid, "CoreTelephony.framework", false);
        proj.AddFrameworkToProject(targetGuid, "CFNetwork.framework", false);

        //写入
        File.WriteAllText(projPath, proj.WriteToString());

        //修改Info.plist
        string plistPath = Path.Combine(pathToBuildProject, "Info.plist");
        PlistDocument plist = new PlistDocument();
        plist.ReadFromFile(plistPath);
        PlistElementDict rootDict = plist.root;

        PlistElementArray urlTypes = rootDict.CreateArray("CFBundleURLTypes");
        var urlItem = urlTypes.AddDict();
        urlItem.SetString("CFBundleTypeRole", "Editor");
        urlItem.SetString("CFBundleURLName", "tencentQQ");
        //1
        urlItem.SetString("CFBundleURLSchemes", "tencent1106988065");
        //2,具体那个有效果
        var schemes = urlItem.CreateArray("CFBundleURLSchemes");
        schemes.AddString("tencent1106988065");

        var urlItem2 = urlTypes.AddDict();
        urlItem2.SetString("CFBundleTypeRole", "Editor");
        urlItem2.SetString("CFBundleURLName", "weixin");
        //1
        urlItem2.SetString("CFBundleURLSchemes", "wxcc165bd58a6d04a0");
        //2,具体那个有效果
        var schemes2 = urlItem2.CreateArray("CFBundleURLSchemes");
        schemes2.AddString("wxcc165bd58a6d04a0");


        //添加白名单
        PlistElementArray queriesSchemes = rootDict.CreateArray("LSApplicationQueriesSchemes");
        queriesSchemes.AddString("mqq");
        queriesSchemes.AddString("mqqapi");
        queriesSchemes.AddString("mqqconnect");
        queriesSchemes.AddString("mqqopensdkapiV3");
        queriesSchemes.AddString("mqqopensdkapiV2");
        queriesSchemes.AddString("mqqopensdkapi");
        queriesSchemes.AddString("mqqOpensdkSSologin");
        queriesSchemes.AddString("weixin");
        queriesSchemes.AddString("wechat");
        queriesSchemes.AddString("weixinULAPI");
        queriesSchemes.AddString("weixinURLParamsAPI");
        queriesSchemes.AddString("NSAppTransportSecurity");
        queriesSchemes.AddString("NSAllowsArbitraryLoads");

        plist.WriteToFile(plistPath);
    }
}
